use std::collections::HashMap;
fn call(number: &str) -> &str {
    match number {
        "798-1364" => "We're sorry, the call cannot be completed as dialed.
            Please hang up and try again.",
        "645-7689" => "Hello, this is Mr. Awesome's Pizza. My name is Fred.
            What can I get for you today?",
        _ => "Hi! Who is this again?"
    }
}
fn main() {
    let mut contacts = HashMap::new();
    contacts.insert("Fred", "645-7689");
    contacts.insert("Tom", "798-1364");
    contacts.insert("Sally", "897-5642");
    contacts.insert("Mary", "987-6543");

//     接受一个引用并返回Option<&V>
    match contacts.get(&"Tom") {
        Some(&number) => println!("Calling Tom: {}\n    {}",number, call(number)),
        _ => println!("Don't have Tom's number"),
    }

//     如果被插入的值为新内容,那么`HashMap::insert`会返回`None`
//     否则返回`Some(value)`
    contacts.insert("Tom", "897-6543");
    println!("Tom's number update: {}", contacts.get(&"Tom").unwrap());
    match contacts.get(&"Fred") {
        Some(&number) => println!("Calling Fred: {}\n   {}",number, call(number)),
        _ => println!("Don't have Fred's number"),
    }
//     删除键值对
    contacts.remove(&("Fred"));
    println!("Fred's number removed: {:?}", contacts.get(&"Fred"));
//     `HashMap::iter()`返回一个迭代器,该迭代器以任意顺序举出所有键值对
//     （&'a key, &'a value）
    for (contact, &number) in contacts.iter() {
        println!("Calling {}: {}\n    {}", contact, number, call(number));
    }
}
